/*
* $Id: QBeeFamilyView.cpp 112 2010-07-07 15:15:39Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "QBeeFamilyView.h"
#include <Database/Operation/QBInfoPointOperation.h>

CQBeeFamilyView::CQBeeFamilyView(CState* pState)
: CQBeeView(pState){}
CQBeeFamilyView::~CQBeeFamilyView(){}

void CQBeeFamilyView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_FAMILY_QBEE;
}

bool CQBeeFamilyView::IsViewState(State state){
	return state.m_nPosTab == JournalEnum::QUEEN_BEE_JOURNAL &&
		state.m_type == JournalEnum::NONE &&
		state.m_nTypeSel == JournalEnum::POINT;
}

void CQBeeFamilyView::Init(){
	m_infoDrawer.Clear();
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CQBInfoPointOperation operation;
	operation.m_content.s_nIdPoint = m_pState->m_nIdParameter;
	IResult* pResult1 = operation.GetValue(pDb);
	std::map<std::tstring, int> m_aAllQB;
	std::map<std::tstring, int> m_aCurQB;
	std::map<std::tstring, int> m_aIdFamily;
	for(int j = 0; j < (int)pResult1->GetSizeResult(); j++){
		CQBInfoPointOperation::content* pContent1 = (CQBInfoPointOperation::content*)(*pResult1)[j];
		std::map<std::tstring, int>::iterator it = m_aCurQB.find(pContent1->s_strNameFamily);
		if(it == m_aCurQB.end())
			it = m_aCurQB.insert(m_aCurQB.end(), std::make_pair(pContent1->s_strNameFamily,  0));
		if(!pContent1->s_bDead)
			it->second = pContent1->s_nCount;
		m_aAllQB[pContent1->s_strNameFamily] += pContent1->s_nCount;
		m_aIdFamily[pContent1->s_strNameFamily] = pContent1->s_nIdFamily;
	}
	pResult1->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);

	for(std::map<std::tstring, int>::iterator it = m_aAllQB.begin(); it != m_aAllQB.end(); it++){
		int j = m_infoDrawer.AddItem(m_aIdFamily[it->first]);
		m_infoDrawer.AddSubItem(j, InfoItem(it->first, _T("Arial"), 20, true, true));
		std::tstring strTemp(IApplication::GetApp()->GetLanguageManager()->GetString(INFO_FAMILY_CUR_COUNT_QB_ID) + _T(" - "));
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromInt(m_aCurQB[it->first]), _T("Arial"), 14, false, false));
		strTemp = IApplication::GetApp()->GetLanguageManager()->GetString(INFO_FAMILY_ALL_QB_ID) + _T(" - ");
		m_infoDrawer.AddSubItem(j, InfoItem(strTemp + CConvertFunction::ConvertFromInt(it->second), _T("Arial"), 14, false, false));
	}
}

BEGIN_MESSAGE_MAP(CQBeeFamilyView, CQBeeView)
	ON_WM_CREATE()
END_MESSAGE_MAP()

int CQBeeFamilyView::OnCreate(LPCREATESTRUCT lpCreateStruct){
	Init();
	return __super::OnCreate(lpCreateStruct);
}